package com.shaobufan.museum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaobufan.museum.mapper.UserExhibitMomentMapper;
import com.shaobufan.museum.mapper.UserMapper;
import com.shaobufan.museum.pojo.entity.UserExhibitMoment;
import com.shaobufan.museum.pojo.vo.MuseumExhibitVo;
import com.shaobufan.museum.pojo.vo.UserExhibitMomentVo;
import com.shaobufan.museum.service.EmailService;
import com.shaobufan.museum.service.MomentService;
import com.shaobufan.museum.service.MuseumExhibitService;
import com.shaobufan.museum.service.UserExhibitMomentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 17126
 * @description 针对表【user_exhibit_moment(用户-展览-时刻 中间表)】的数据库操作Service实现
 * @createDate 2024-04-07 11:09:02
 */
@Service
public class UserExhibitMomentServiceImpl extends ServiceImpl<UserExhibitMomentMapper, UserExhibitMoment>
        implements UserExhibitMomentService {

    @Resource
    UserExhibitMomentMapper userExhibitMomentMapper;

    @Resource
    MuseumExhibitService museumExhibitService;
    @Resource
    MomentService momentService;
    @Resource
    EmailService emailService;
    @Resource
    UserMapper userMapper;

    /**
     * 添加用户参展日程
     *
     * @param userId 用户id
     * @param exId   展览id
     * @param moId   时刻id
     * @return Boolean
     */
    @Override
    public Boolean addUserExhibitMoment(Integer userId, Integer exId, Integer moId) {
        String userAccount = userMapper.selectById(userId).getUserAccount();
        String userEmail = userMapper.selectById(userId).getUserEmail();
        String exName = museumExhibitService.searchExhibitById(exId).getExName();
        LocalTime moment = momentService.searchMoment(moId).getMoment();

        UserExhibitMoment userExhibitMoment = new UserExhibitMoment();
        userExhibitMoment.setUserId(userId);
        userExhibitMoment.setExId(exId);
        userExhibitMoment.setMoId(moId);

        String text = "尊敬的" + userAccount + "，线上博物馆提醒您，您已成功预约在" + moment + "点的《" + exName + "》，祝您参展愉快！";
        emailService.sendCommonEmail(userEmail, "预约《" + exName + "》成功通知", text);
        int inserted = userExhibitMomentMapper.insert(userExhibitMoment);
        return inserted == 1;
    }

    /**
     * 通过用户 id获取参展日程
     *
     * @param userId 用户 id
     * @return List<UserExhibitMomentVo>
     */
    @Override
    public List<UserExhibitMomentVo> searchUserExhibitMoment(Integer userId) {
        QueryWrapper<UserExhibitMoment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return getUserExhibitMomentVoList(userExhibitMomentMapper.selectList(queryWrapper));
    }

    @Override
    public List<MuseumExhibitVo> searchUserExhibitByUserid(Integer userId) {
        return searchUserExhibitMoment(userId).stream().map(userExhibitMomentVo -> museumExhibitService.searchExhibitById(userExhibitMomentVo.getExId())).collect(Collectors.toList());
    }

    public List<UserExhibitMomentVo> getUserExhibitMomentVoList(List<UserExhibitMoment> userExhibitMomentList) {
        return userExhibitMomentList.stream().map(this::getUserExhibitMomentVo).collect(Collectors.toList());
    }

    public UserExhibitMomentVo getUserExhibitMomentVo(UserExhibitMoment userExhibitMoment) {
        UserExhibitMomentVo userExhibitMomentVo = new UserExhibitMomentVo();
        userExhibitMomentVo.setUserId(userExhibitMoment.getUserId());
        userExhibitMomentVo.setExId(userExhibitMoment.getExId());
        userExhibitMomentVo.setMoId(userExhibitMoment.getMoId());

        userExhibitMomentVo.setExName(museumExhibitService.searchExhibitById(userExhibitMoment.getExId()).getExName());
        userExhibitMomentVo.setMoment(momentService.searchMoment(userExhibitMoment.getMoId()).getMoment());

        return userExhibitMomentVo;
    }
}